from flask import Blueprint,request,make_response,jsonify,redirect
from flask_restful import Resource,Api
import json
from application.models.users.user import *
from application.models.users.user import db
from utils.Myjwt import myjwt
import functools,time
import requests
from db import db
work_blue=Blueprint("work",__name__)
api=Api(work_blue)

@work_blue.route("/setwork",methods=["POST"])
def setwork():
    data=json.loads(request.data)
    name=data["name"]
    sql="insert into work_flow(name) value('%s')"%(name)
    db.add(sql)
    db.commit()
    return jsonify({"code":200,"msg":"添加成功"})
# 获取所有工作流
@work_blue.route("/getwork")
def getwork():
    sql="select * from work_flow"
    res=db.findAll(sql)
    return jsonify({"code":200,"msg":res})
# 工作流添加流程
@work_blue.route("/uwork",methods=["POST"])
def uwork():
    data=json.loads(request.data)
    wid=int(data["wid"])
    params=data["params"]
    sql="update work_flow set params='%s' where id=%d"%(params,wid)
    db.add(sql)
    db.commit()
    return jsonify({"code":200,"msg":"ok"})
# 获取工作流流程
@work_blue.route("/getworks")
def getworks():
    id=request.args.get("id")
    sql="select params from work_flow where id =%d"%(int(id))
    res=db.findone(sql)
    return jsonify({"code":200,"data":res["params"]})
# 获取所有角色
@work_blue.route("/getrole1")
def getrole1():
    sql="select * from roles1"
    res=db.findAll(sql)
    return jsonify({"code":200,"lst":res})
# 审批权限添加
@work_blue.route("/uaudit",methods=["POST"])
def uaudit():
    data=json.loads(request.data)
    wid=int(data["wid"])
    auditlist=data["auditlist"]
    sql="update work_flow set auditlist='%s' where id=%d"%(auditlist,wid)
    db.add(sql)
    db.commit()
    return jsonify({"code":200,"msg":"ok"})